package com.sharedmobility.dao;

import com.sharedmobility.model.User;
import com.sharedmobility.util.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.math.BigDecimal;

public class UserDAO {
    
    private Connection getConnection() throws SQLException {
        return DBUtil.getConnection();
    }
    
    public User findById(Integer id) throws SQLException {
        String sql = "SELECT * FROM users WHERE id = ?";
        try (Connection conn = getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setInt(1, id);
            try (ResultSet rs = stmt.executeQuery()) {
                if (rs.next()) {
                    return mapResultSetToUser(rs);
                }
            }
        }
        return null;
    }
    
    public User findByUsername(String username) {
        String sql = "SELECT * FROM users WHERE username = ?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            
            stmt.setString(1, username);
            ResultSet rs = stmt.executeQuery();
            
            if (rs.next()) {
                return mapResultSetToUser(rs);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    
    public boolean create(User user) {
        String sql = "INSERT INTO users (username, password, phone, balance, credit_score, status, role) " +
                    "VALUES (?, ?, ?, ?, ?, ?, ?)";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            
            stmt.setString(1, user.getUsername());
            stmt.setString(2, user.getPassword());
            stmt.setString(3, user.getPhone());
            stmt.setBigDecimal(4, user.getBalance());
            stmt.setInt(5, user.getCreditScore());
            stmt.setInt(6, user.getStatus());
            stmt.setString(7, user.getRole());
            
            return stmt.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }
    
    public boolean update(User user) throws SQLException {
        String sql = "UPDATE users SET username = ?, phone = ?, password = ?, balance = ?, credit_score = ? WHERE id = ?";
        try (Connection conn = getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setString(1, user.getUsername());
            stmt.setString(2, user.getPhone());
            stmt.setString(3, user.getPassword());
            stmt.setBigDecimal(4, user.getBalance());
            stmt.setInt(5, user.getCreditScore());
            stmt.setInt(6, user.getId());
            return stmt.executeUpdate() > 0;
        }
    }
    
    public List<User> findAll() {
        List<User> users = new ArrayList<>();
        String sql = "SELECT * FROM users";
        try (Connection conn = DBUtil.getConnection();
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery(sql)) {
            
            while (rs.next()) {
                users.add(mapResultSetToUser(rs));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return users;
    }
    
    private User mapResultSetToUser(ResultSet rs) throws SQLException {
        User user = new User();
        user.setId(rs.getInt("id"));
        user.setUsername(rs.getString("username"));
        user.setPassword(rs.getString("password"));
        user.setPhone(rs.getString("phone"));
        user.setBalance(rs.getBigDecimal("balance"));
        user.setCreditScore(rs.getInt("credit_score"));
        user.setStatus(rs.getInt("status"));
        user.setCreateTime(rs.getTimestamp("create_time"));
        user.setIsAdmin(rs.getBoolean("is_admin"));
        user.setIsMaintenanceStaff(rs.getBoolean("is_maintenance_staff"));
        user.setIsDriver(rs.getBoolean("is_driver"));
        user.setIsCustomerService(rs.getBoolean("is_customer_service"));
        user.setRole(rs.getString("role"));
        user.setAddtime(rs.getTimestamp("addtime"));
        return user;
    }
} 